package com.mybatis.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mybatis.Dao.IMemberMapper;
import com.mybatis.Util.MyBatisUtil;
import com.mybatis.Util.Result;
import com.mybatis.entity.Employee;

/**
 * Servlet implementation class updateEmployeeServlet
 */
@WebServlet("/updateEmployeeServlet")
public class updateEmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public updateEmployeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");//设置输出信息编码方式为utf-8
		response.setContentType("text/json");//设置输出信息格式
		String key = request.getParameter("key");//获取要修改的姓名
		String sex = request.getParameter("sex");//获取要修改的性别
		String dptIdStr = request.getParameter("dep");//获取要修改的部门ID
		String bir = request.getParameter("bir");//获取要修改的生日
		String idStr = request.getParameter("id");//获取要修改的员工工号
		//将获取到的信息传入到一个员工对象中
		Employee emp = new Employee();
		if(idStr!=null&&!idStr.equals("")) {
			int id = Integer.parseInt(idStr);
			emp.setId(id);
		}
		if(dptIdStr!=null&&!dptIdStr.equals("")) {
			int dptId = Integer.parseInt(dptIdStr);
			emp.setDptId(dptId);
		}
		emp.setName(key);
		emp.setSex(sex);
		emp.setBirthday(bir);
		//调用修改方法
		IMemberMapper mdao = MyBatisUtil.getMapper(IMemberMapper.class);
		int num = mdao.update(emp);
		//将执行结果存入结果集
		Result rs = new Result(num,"成功",num,null);
		//将结果集转为json格式
		Gson g= new Gson();
		String json = g.toJson(rs);
		//输出结果集
		PrintWriter out =response.getWriter();
		out.print(json);
		//释放资源
		out.close();
		MyBatisUtil.closeSqlSession();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
